#include "MainHeader.h"

BOOL CALLBACK   LPProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){

   HDC             hdc;
   int             i = 0;
   u16             wndrs[3] = {hsbC1, hsbC2, hsbC2};
   PAINTSTRUCT     ps;

   switch (message)
   {

   case WM_INITDIALOG:
      hwndLPE = hwnd;

      break;

   case WM_PAINT:

      hdc = BeginPaint(hwnd, &ps);

      DrawPal(hdc, GameEngine->Members.PcPalMem, 0, 0, 0);

      EndPaint(hwnd, &ps);

      break;
   case WM_TIMER:

      break;

   case WM_LBUTTONDOWN:
      // Check for the pal rect
      if ((GetX(lParam) >= 256) || (GetY(lParam) >= 256))
         return 0;
      LoadColor((GetX(lParam) / 16) + (GetY(lParam) / 16) * 16);
      break;
   case WM_COMMAND:
      switch (LOWORD(wParam))
      {
      case cmdSaveColors:

         GBA.EncodePal(GBA.GBAPal, GameEngine->Members.PcPalMem, 16, 0);
      
		 fseek(GBA.ROM, GameEngine->Members.RoomTilesets[GameEngine->Members.RoomHeader->bTileset].pTiles - 0x8000000, SEEK_SET); // Seek 
                                                                                              // the 
                                                                                              // endcoded 
                                                                                              // pal

            fwrite(&GBA.GBAPal[32], sizeof(short), 0x1E0, GBA.ROM); // Read

                                                                    // pal c

         
         if (GameEngine->isZM())
         {
            fseek(GBA.ROM, 0x31C72E, SEEK_SET);
            fwrite(&GBA.GBAPal[1], sizeof(short), 15, GBA.ROM);

            fseek(GBA.ROM, 0x3ED51C, SEEK_SET);
            fwrite(&GBA.GBAPal[16], sizeof(short), 16, GBA.ROM);

         } else if (GameEngine->isMF())
         {

            fseek(GBA.ROM, 0x40805E, SEEK_SET);
            fwrite(&GBA.GBAPal[1], 2, 47, GBA.ROM);

         }
         GBA.DecodePal(GBA.GBAPal, GameEngine->Members.PcPalMem, 16, 0);
         InvalidateRect(hwndLPE, 0, 1);

         break;
      case cmdANC:
         DrawTileset();
         ThisBackLayer->BImage.SetPalette(GameEngine->Members.PcPalMem);
         ThisForeground->BImage.SetPalette(GameEngine->Members.PcPalMem);
         ThisBackLayer->BImage.SetPalette(GameEngine->Members.PcPalMem);
         ThisLevelData->BImage.SetPalette(GameEngine->Members.PcPalMem);
         ThisBackground->BImage.SetPalette(GameEngine->Members.PcPalMem);
		
         DrawRoom();
         InvalidateRect(hwndTileset, 0, 1);
         InvalidateRect(hwndMap, 0, 1);
         break;
      case IDCANCEL:
         EndDialog(hwnd, 1);
         break;
      }

      break;
   case WM_HSCROLL:

      if ((HWND) lParam == GetDlgItem(hwnd, hsbC1))
      {
         scrColors[0].UpdateScroll(wParam);
      }
      if ((HWND) lParam == GetDlgItem(hwnd, hsbC2))
      {

         scrColors[1].UpdateScroll(wParam);
      }
      if ((HWND) lParam == GetDlgItem(hwnd, hsbC3))
      {
         scrColors[2].UpdateScroll(wParam);
      }
      ChangeColor();
      break;

   }
   return 0;
}

int             LoadColor(long Pal)
{
   RGBQUAD         Colors;
   int             i = 0;
   char            blahstr[32] = {0};
   HWND            hwndArray[3] = {GetDlgItem(hwndLPE, lblC1), GetDlgItem(hwndLPE, lblC2), GetDlgItem(hwndLPE, lblC3)};

//Hello welcome to load color. 
   curclr = Pal;
   Colors.rgbRed   = GetRValue((u32) GameEngine->Members.PcPalMem[curclr]); /*
                                                       * get R, G, and B out
                                                       * of DWORD 
                                                       */
   Colors.rgbGreen = GetGValue((u32) GameEngine->Members.PcPalMem[curclr]);
   Colors.rgbBlue  = GetBValue((u32) GameEngine->Members.PcPalMem[curclr]);
   Colors.rgbReserved = 0;

   scrColors[0].curpos = Colors.rgbRed;
   scrColors[1].curpos = Colors.rgbGreen;
   scrColors[2].curpos = Colors.rgbBlue;
   SetScrollPos(scrColors[0].me, SB_CTL, scrColors[0].curpos, 1);
   SetScrollPos(scrColors[1].me, SB_CTL, scrColors[1].curpos, 1);
   SetScrollPos(scrColors[2].me, SB_CTL, scrColors[2].curpos, 1);
//Do labels
   for (i = 0; i < 3; i++)
   {
      sprintf(blahstr, "RGB: %d\nGBA:%d", scrColors[i].curpos, (scrColors[i].curpos >> 3) & 0x1F);

      SetWindowText(hwndArray[i], blahstr);

   }
   return 0;
}

int             ChangeColor()
{
   int             i = 0;
   char            blahstr[32] = {0};
   HWND            hwndArray[3] = {GetDlgItem(hwndLPE, lblC1), GetDlgItem(hwndLPE, lblC2), GetDlgItem(hwndLPE, lblC3)};

   for (i = 0; i < 3; i++)
   {
      sprintf(blahstr, "RGB: %d\nGBA:%d"
              ,scrColors[i].curpos

              ,(scrColors[i].curpos >> 3) & 0x1F);

      SetWindowText(hwndArray[i], blahstr);
      GameEngine->Members.PcPalMem[curclr] = RGB(scrColors[0].curpos, scrColors[1].curpos, scrColors[2].curpos);
      InvalidateRect(hwndLPE, 0, 0);
//DrawRoom();
   }
   return 0;

};
